package com.example.chenduoyun.safe.activity;
/**
 * @author cdy
 */

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.chenduoyun.safe.R;
import com.example.chenduoyun.safe.utils.ConstantValue;
import com.example.chenduoyun.safe.utils.SpUtils;
import com.example.chenduoyun.safe.utils.ToastUtil;

public class Setup4Activity extends BaseSetupActivity implements View.OnClickListener {

    private CheckBox cb_box;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup4);
        initUI();

        findViewById(R.id.btn_next).setOnClickListener(this);
        findViewById(R.id.btn_pre).setOnClickListener(this);

    }

    @Override
    public void showNextPage() {
        judgeNextPage();
    }

    @Override
    public void showPrePage() {
        judgePerPage();
    }

    private void initUI() {
        cb_box = findViewById(R.id.cb_box);
        boolean open_security = SpUtils.getBoolean(getApplicationContext(), ConstantValue.OPEN_SECURITY, false);
        if (open_security) {
            cb_box.setText("安全设置已开启");
        } else {
            cb_box.setText("安全设置已关闭");
        }
        cb_box.setChecked(open_security);
        cb_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SpUtils.putBoolean(getApplicationContext(), ConstantValue.OPEN_SECURITY, isChecked);
                if (isChecked) {
                    cb_box.setText("安全设置已开启");
                } else {
                    cb_box.setText("安全设置已关闭");
                }

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_pre:
                judgePerPage();
                break;
            case R.id.btn_next:
                judgeNextPage();
                break;
            default:
                break;
        }
    }

    private void judgePerPage() {
        Intent intent = new Intent(getApplicationContext(), Setup3Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);
    }

    private void judgeNextPage() {
        if (SpUtils.getBoolean(getApplicationContext(), ConstantValue.OPEN_SECURITY, false)) {
            Intent intent = new Intent(getApplicationContext(), MobileSafeActivity.class);
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
            SpUtils.putBoolean(getApplicationContext(), ConstantValue.SETUP_OVER, true);
        } else {
            ToastUtil.show(getApplicationContext(), "请开启防盗保护设置");
        }
    }
}
